package com.example.filesftp.ftptransfer.controller;

import com.example.common.core.util.R;
import com.example.filesftp.ftptransfer.domain.entity.FileTransferConfig;
import com.example.filesftp.ftptransfer.service.FileSftpService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author chenhuan
 */
@Slf4j
@RestController
@RequestMapping("/fliesftp")
@RequiredArgsConstructor
public class FileSftpServiceController {


    private final FileSftpService fileSftpService;


    @GetMapping("/getAllConfigs")
    public List<FileTransferConfig> getAllConfigs() {
        return fileSftpService.getAllConfigs();
    }

    @GetMapping("/{id}")
    public FileTransferConfig getConfigById(@PathVariable String id) {
        return fileSftpService.getConfigById(id);
    }

    @PostMapping
    public FileTransferConfig saveConfig(@RequestBody FileTransferConfig config) throws Exception {
        return fileSftpService.saveConfig(config);
    }

    @DeleteMapping("/{id}")
    public void deleteConfig(@PathVariable String id) {
        fileSftpService.deleteConfig(id);
    }

    @PostMapping("/{id}/trigger")
    public void triggerJob(@PathVariable String id) {
        fileSftpService.triggerJob(id);
    }


    @GetMapping("/downloadUserManual")
    public void downLoadUserManual(HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileSftpService.downloadUserManual(request,response);
    }


    /**
//     * ftp从111获取夸父版本功率预测文件，并上传到指定ftp目录
//     * @param date
//     * @return
//     */
//    @GetMapping("/getForeFileAndUpload")
//    public R getForeFileAndUpload(LocalDate date)
//    {
//        log.info("---------------------getForeFileAndUpload定时任务开始---------------------------");
//        if(date == null){
//            date = LocalDate.now();
//        }
//        R r = fileSftpService.getForeFileAndUpload(date);
//        if(r.getCode()==0){
//            log.info("下载任务执行完成!");
//        }
//
//        R uploadR = fileSftpService.uploadCsvFileToFtp(date);
//        if (uploadR.getCode() == 0){
//            log.info("上传任务执行完成!");
//        }
//        log.info("---------------------getForeFileAndUpload定时任务结束---------------------------");
//        return R.ok(null,"执行成功");
//    }
}
